package edu.towson.cosc.cosc455.jyoun.project1;
import java.io.*;

public class MyCompiler {

	public static String token;
	public static MySyntaxAnalyzer syntax = new MySyntaxAnalyzer();
	public static MyLexicalAnalyzer lexical = syntax.lexer;
	public static SemanticAnalyzer semantic = new SemanticAnalyzer();
	
	public static void main(String[] args) throws CompilerException, IOException, NullPointerException{
		String testfiles;
		String thisfilename;
	
		if (args.length == 1) {
			if (args[0].endsWith(".mkd")) {		
				testfiles = readFile(args[0]);
				thisfilename = args[0].replace(".mkd", "");
				token = "";
				lexical.thishtml(testfiles);
				syntax.document();
				semantic.Semantic();
				PrintWriter output = new PrintWriter(thisfilename + ".html");
				for(int x = 0; x < semantic.htmlfile.size(); x++) 
					output.println(semantic.htmlfile.get(x));
			}
			else
				System.out.println("ERROR: Please only use files with .mkd extension.");
		}
		else if (args.length >= 1)
			System.out.println("ERROR: Only one file can be used as input.");
		else
			System.out.println("ERROE: A file with .mkd extension is needed.");
	}
	
	private static String readFile(String fileName) throws IOException {
	    BufferedReader br = new BufferedReader(new FileReader(fileName));
	        StringBuilder sb = new StringBuilder();
	        String line = br.readLine();

	        while (line != null) {
	            sb.append(line);
	            line = br.readLine();
	        }
	        br.close();
	        return sb.toString();
	}
			//1. check to make sure args check
			//2. check to make sure the file has a mkd extension --> endsWith(".mkd"); check
			//3. inits  check
			//4. call the lexical analyzer to get the first token set check
			//5. call the start state of the parser, i.e., document(); check
			// ... lexer and parser does its thing 
			//6. call the semantic analyzer check 
			//7. write the output file to html check 
			//8. clean up. check
}